import 'package:flutter/material.dart';
import 'package:wayofdressing/contraints.dart';
import 'package:wayofdressing/vivo_connect.dart';
import 'package:wayofdressing/all_information/user_information.dart';
import 'package:wayofdressing/components/inmenubutton.dart';

class BodyManageScreen extends StatefulWidget {
  static final String id = 'bodymanagescreen';

  const BodyManageScreen({super.key});

  @override
  State<BodyManageScreen> createState() => _BodyManageScreenState();
}

class _BodyManageScreenState extends State<BodyManageScreen> {
  int height = 170;
  int weight = 60;
  int waist = 80;
  int bustSize = 80;
  int hip = 80;
  int age=18;
  Future<void> aiModeling() async {
    final aiSuggestion = await vivogpt(
        '这是一组数据对应用户的三维和身高体重等信息,年龄:${age<16?18:age},性别:${gender=='无'?'男':gender},身高:$height,体重:$weight,腰围:$waist,胸围:$bustSize,臀围:$hip,请你根据这组信息计算出用户的腰臀比，胸腰比，臀胸比，腰身高比，臀身高比，胸身高比，BMI，并推测体脂率，以及身材比例是什么类型。请只给出对应的数据，不要回复其他内容');
    print(aiSuggestion);

    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20),
            side: BorderSide(color: Colors.indigo, width: 3),
          ),
          backgroundColor: Colors.white,
          title: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '已保存',
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 24.0,
                ),
              ),
              TextButton(
                child: Icon(
                  Icons.cancel_outlined,
                  color: Colors.black54,
                  size: 40,
                ),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          ),
          content: Container(
            height: MediaQuery.of(context).size.height - 500,
            child: ListView(
              children: [
                Center(child: Text('Ai辅助建模推测信息如下(AI还待调优，仅为辅助建模示例)：')),
                Text(
                  aiSuggestion,
                  softWrap: true,
                ),
              ],
            ),
          ),
        );
      },
    );
  }
@override
  void initState() {
    // TODO: implement initState
    super.initState();
    try{
      age=DateTime.now().year-DateTime.parse(birthday=='未设置'?'2024-07-19':birthday).year;
    }catch(e){
      print(e);
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xfffffaf0),
      appBar: AppBar(
        backgroundColor: Color(0xfffffaf0),
        elevation: 0,
        toolbarHeight: 80,
        leadingWidth: 100,
        centerTitle: true,
        title: Text(
          '尺码管理',
          style: TextStyle(
            color: Colors.black,
            fontWeight: FontWeight.bold,
            fontSize: kTopTitleFontSize,
          ),
        ),
        leading: Row(
          children: [
            SizedBox(
              width: 10,
            ),
            GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: Transform.scale(
                scaleX: -1,
                child: Icon(
                  Icons.play_arrow_rounded,
                  size: kAppBarButtonSize,
                  color: Colors.black.withOpacity(0.5),
                ),
              ),
            ),
          ],
        ),
        actions: [
          MenuAnchor(
            menuChildren: [
              InMenuButton(
                onPressed: () {},
                text: '待开发',
                alignment: Alignment.centerRight,
              ),
            ],
            style: MenuStyle(
              shape: MaterialStateProperty.all(
                RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(25.0),
                ),
              ),
              alignment: Alignment(-3, 0.5),
              padding: MaterialStateProperty.all(EdgeInsets.all(15.0)),
              shadowColor: MaterialStateProperty.all(Colors.transparent),
              backgroundColor: MaterialStateProperty.all(
                Colors.black.withOpacity(0.4),
              ),
            ),
            builder: (BuildContext context, MenuController controller,
                Widget? child) {
              return GestureDetector(
                onTap: () {
                  if (controller.isOpen) {
                    controller.close();
                  } else {
                    controller.open();
                  }
                },
                child: Icon(
                  Icons.keyboard_control_rounded,
                  size: kAppBarButtonSize,
                  color: Colors.black,
                ),
              );
            },
          ),
          SizedBox(
            width: 20,
          )
        ],
      ),
      body: Container(
        margin: EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          children: [
            Expanded(
              child: Row(
                children: [
                  Expanded(
                    child: BodyManageButton(
                      unit: 'kg',
                      content: weight,
                      title: '体重',
                    ),
                  ),
                  Expanded(
                    child: BodyManageButton(
                      unit: 'cm',
                      content: waist,
                      title: '腰围',
                    ),
                  )
                ],
              ),
            ),
            Expanded(
              child: Container(
                margin: EdgeInsets.symmetric(vertical: 15, horizontal: 10),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(25),
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      spreadRadius: 2,
                      blurRadius: 5,
                      color: Colors.black.withOpacity(0.2),
                      offset: Offset(0, 2),
                    )
                  ],
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        TextButton(
                          onPressed: () {
                            aiModeling();
                          },
                          child: Text(
                            '保存',
                            style: TextStyle(fontSize: 18, color: Colors.black),
                          ),
                        )
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.baseline,
                      textBaseline: TextBaseline.alphabetic,
                      children: [
                        Icon(
                          Icons.accessibility_new_outlined,
                          size: 60,
                        ),
                        Text(
                          '身高',
                          style: TextStyle(fontSize: 24),
                        )
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.baseline,
                      textBaseline: TextBaseline.alphabetic,
                      children: [
                        Text(
                          '$height',
                          style: TextStyle(fontSize: 28, color: Colors.black),
                        ),
                        Text(
                          'cm',
                          style: TextStyle(fontSize: 16),
                        )
                      ],
                    ),
                    Slider(
                      min: 100,
                      max: 240,
                      activeColor: Colors.black.withOpacity(0.7),
                      inactiveColor: Colors.black12,
                      label: '$height',
                      value: height.toDouble(),
                      onChanged: (double newValue) {
                        setState(() {
                          height = newValue.round();
                        });
                      },
                    ),
                  ],
                ),
              ),
            ),
            Expanded(
                child: Row(
              children: [
                Expanded(
                    child: BodyManageButton(
                      unit: 'cm',
                  content: bustSize,
                  title: '胸围',
                )),
                Expanded(
                    child: BodyManageButton(
                  content: hip,
                  title: '臀围',
                      unit: 'cm',
                )),
              ],
            )),
          ],
        ),
      ),
    );
  }
}

class BodyManageButton extends StatefulWidget {
  BodyManageButton({
    required this.title,
    required this.content,
    required this.unit,
  });

  final String title;
  int content;
  String unit;

  @override
  State<BodyManageButton> createState() => _BodyManageButtonState();
}

class _BodyManageButtonState extends State<BodyManageButton> {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 15, horizontal: 10),
      decoration: BoxDecoration(boxShadow: [
        BoxShadow(
          spreadRadius: 2,
          blurRadius: 5,
          color: Colors.black.withOpacity(0.2),
          offset: Offset(0, 2),
        )
      ], borderRadius: BorderRadius.circular(25), color: Colors.white),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            widget.title,
            style: TextStyle(
              fontSize: 24,
            ),
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.baseline,
            textBaseline: TextBaseline.alphabetic,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('${widget.content}',
                  style: TextStyle(
                    fontSize: 20,
                  )),
              Text(widget.unit)
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              GestureDetector(
                onTap: () {
                  setState(() {
                    widget.content -= 1;
                  });
                },
                child: CircleAvatar(
                  backgroundColor: Colors.grey[300],
                  child: Icon(
                    Icons.remove,
                    color: Colors.black,
                  ),
                ),
              ),
              SizedBox(
                width: 8,
              ),
              GestureDetector(
                onTap: () {
                  setState(() {
                    widget.content += 1;
                  });
                },
                child: CircleAvatar(
                  backgroundColor: Colors.grey[300],
                  child: Icon(
                    Icons.add_rounded,
                    color: Colors.black,
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }
}
